<template>
  <div>
    <zh-form  ref="formInfo" :colmun="1" :rules="rules" :model="formInfo"  labelAlign="right" >

      <zh-card style="width:100%">
        <zh-row>
          <zh-input style="float: left"  type="text"  :span="22"  prop="c0302" v-model="formInfo.c0302" label="任务名称"
                    :labelCol="{span:3}" :wrapperCol="{span:18}"/>
        </zh-row>
        <zh-row :span="24">
          <zh-input style="float: left" prop="c0303" :span="11" type="select" :defaultFields='{label:"dictValue",value:"dictKey",}'
                    :options="dictData.c0303" label="任务表" v-model="formInfo.c0303"
                    :labelCol="{span:6}" :wrapperCol="{span:12}"></zh-input>
        </zh-row>
        <zh-row>
          <zh-input prop="taskArea" type="tree" mode="multiple" label="任务范围"  placeholder="支持多选"
                    allowClear
                    tree-checkable
                    multiple
                    v-model="formInfo.taskArea"
                    :treeData="taskAreaTree"
                    :showCheckedStrategy = "SHOW_PARENT"
                    :span="22"
                    style="float: left"
                    :labelCol="{span:3}" :wrapperCol="{span:18}"
          >
          </zh-input>
        </zh-row>

        <zh-row>
          <zh-input :span="22" prop="c0304Time"  type="range"  label="起止时间" v-model="formInfo.c0304Time"
                    :labelCol="{span:3}" :wrapperCol="{span:18}" :disabledDate="onDisabledDate"></zh-input>
        </zh-row>

      </zh-card>

    </zh-form>
  </div>
</template>

<script>
import modal from 'ant-design-vue/es/modal'
import { getBatchDict} from "@/services/system/api"
import {getPermissionParams,checkTaskAdd,TasksAddAndOpen
} from '@/services/system/whdngxlApi'
import {TreeSelect} from "ant-design-vue";
import moment from "moment";
const SHOW_PARENT = TreeSelect.SHOW_PARENT;
export default {
  components:{

  },
  props:{
    listQuery:{
      type:Function,
      default:null
    }
  },
  data() {
    return {
      formInfo:{
        c0302:'',
        c0303:'',
        taskArea:undefined,
        c0304Time:[],
      },
      rules:{},
      dictData: {
        c0303:[],
      },
      taskAreaDict:{},
      taskAreaTree:[],
      SHOW_PARENT,
    }
  },

  created() {
    const param = {}
    param.dictFields = 'c0303'
    this.$http.post(getBatchDict, param).then(res => {
      this.dictData = res.result
    })
    //获取任务范围
    this.$http.post(getPermissionParams, {}).then(res => {
      const data = res.result
      this.taskAreaTree = data.orgTreeData
    })
    this.initDate()
  },
  methods: {
    initDate(){

    },
    //日期选择器禁用的日期
    onDisabledDate(current){
      const time = moment().endOf('day').subtract(1, 'days')
      return current < time
    },
    taskSave(){
      if(this.formInfo.c0302 == "" || this.formInfo.c0302 == null){
        modal.warn({title: '提示', content: '任务名称不能为空'})
        return
      }
      if(this.formInfo.c0303 == "" || this.formInfo.c0303 == null){
        modal.warn({title: '提示', content: '任务表不能为空'})
        return
      }
      if(this.formInfo.taskArea == undefined){
        modal.warn({title: '提示', content: '任务范围不能为空'})
        return
      }
      if(this.formInfo.c0304Time.length === 0){
        modal.warn({title: '提示', content: '任务时间不能为空'})
        return
      }
      this.$loading.show('正在处理数据,请等待...')
      const params = Object.assign(this.formInfo)
      setTimeout(() => {
        this.$http.post(checkTaskAdd, params).then((res) => {
          this.$message.info('保存成功')
          this.$emit("handleCancel")
          if(this.listQuery){
            this.listQuery()
          }
          this.$loading.hide()
        }).catch((error) => {
          console.log(JSON.stringify(error))
          this.$loading.hide()
        }).finally(() =>{
        })
      }, 50)
    },
    //保存并开启
    addAndOpen(){
      if(this.formInfo.c0302 == "" || this.formInfo.c0302 == null){
        modal.warn({title: '提示', content: '任务名称不能为空'})
        return
      }
      if(this.formInfo.c0303 == "" || this.formInfo.c0303 == null){
        modal.warn({title: '提示', content: '任务表不能为空'})
        return
      }
      if(this.formInfo.taskArea == undefined){
        modal.warn({title: '提示', content: '任务范围不能为空'})
        return
      }
      if(this.formInfo.c0304Time.length === 0){
        modal.warn({title: '提示', content: '任务时间不能为空'})
        return
      }
      this.$loading.show('正在处理数据,请等待...')
      const params = Object.assign(this.formInfo)
      setTimeout(() => {
        this.$http.post(TasksAddAndOpen, params).then((res) => {
          this.$message.info('保存成功')
          this.$emit("handleCancel")
          if(this.listQuery){
            this.listQuery()
          }
          this.$loading.hide()
        }).catch((error) => {
          console.log(JSON.stringify(error))
          this.$loading.hide()
        }).finally(() =>{
        })
      }, 50)
    },

  }
}
</script>

<style scoped>

</style>